package cn.edu.nju.software.zp13.assignment5.service;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

public class SOAPAPIExample {

	public static void main(String[] args) throws Exception {
		SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
		SOAPPart soapPart = soapMessage.getSOAPPart();
		SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

		SOAPHeader soapHeader = soapEnvelope.getHeader();
		@SuppressWarnings("unused")
		SOAPHeaderElement headerElement = soapHeader.addHeaderElement(
				soapEnvelope.createName("Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));

		SOAPBody soapBody = soapEnvelope.getBody();
		soapBody.addAttribute(
				soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body");
		Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com");
		@SuppressWarnings("unused")
		SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
		Source source = soapPart.getContent();
		Transformer transformer=TransformerFactory.newInstance().newTransformer();
		transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.transform(source, new StreamResult(System.out));
	}
}